package com.satan.vote.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.satan.vote.dto.VoteDetailDTO;
import com.satan.vote.entity.VoteDetail;
import com.satan.vote.entity.VoteItem;
import com.satan.vote.entity.WxUserInfo;
import com.satan.vote.mapper.VoteDetailMapper;
import com.satan.vote.mapper.VoteItemMapper;
import com.satan.vote.service.VoteDetailService;
import com.satan.vote.service.VoteItemService;
import com.satan.vote.service.WxUserInfoService;
import com.satan.vote.util.JwtUtils;
import com.satan.vote.vo.ResultVO;
import com.satan.vote.vo.VoteDetailVO;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName VoteDeatailImpl
 * @Description TODO
 * @date 2023/8/14 18:05
 * @Version 1.0
 */
@Service
public class VoteDetailImpl extends ServiceImpl<VoteDetailMapper, VoteDetail> implements VoteDetailService {
    @Autowired
    private VoteItemService voteItemService;
    @Autowired
    private VoteItemMapper voteItemMapper;
    @Autowired
    private WxUserInfoService wxUserInfoService;
    @Override
    @Transactional
    public ResultVO add(VoteDetailDTO voteDetailDTO, String token) {
        System.out.println("token=" + token);
        Claims claims = JwtUtils.validateJWT(token).getClaims();
        System.out.println("openid=" + claims.getId());
        String openid = claims.getId();
//        new QueryWrapper<VoteDetail>().eq("openid", openid).eq("vote_id", voteDetail.getVoteId())
        QueryWrapper queryWrapper = QueryWrapper.create().where(VoteDetail::getOpenid).eq(openid).where(VoteDetail::getVoteId).eq(voteDetailDTO.getVoteId());
        long count = count(queryWrapper);
        Map<String, Object> resultMap = new HashMap<>();
        if (count > 0) {
            resultMap.put("info", "您已经投票过，不能重复投票！");
            return ResultVO.fail(500, "您已经投票过，不能重复投票！");
        } else {
            resultMap.put("info", "投票成功！");
            voteDetailDTO.setOpenid(openid);
            voteDetailDTO.setVoteDate(LocalDateTime.now());
// 对指定投票项的number+1操作

//            voteItemService.update(new UpdateWrapper<VoteItem>().setSql("number=number+1").eq("id",voteDetail.getVoteItemId()));
            VoteItem voteItem = UpdateEntity.of(VoteItem.class);
            UpdateWrapper updateWrapper = UpdateWrapper.of(voteItem);
            updateWrapper.setRaw("number", "number+1");
            QueryWrapper queryWrapper1 = QueryWrapper.create().where(VoteDetail::getId).eq(voteDetailDTO.getVoteItemId());
            // 更新投票记录)
            voteItemService.update(voteItem, queryWrapper1);
            // 保存投票记录
            VoteDetail voteDetail = new VoteDetail();
            BeanUtil.copyProperties(voteDetailDTO, voteDetail);
            save(voteDetail);
            return ResultVO.success(null);
        }
    }

    @Override
    public ResultVO findById(Integer voteId, Integer voteItemId) {
        QueryWrapper queryWrapper = QueryWrapper.create().where(VoteDetail::getVoteId).eq(voteId).and(VoteDetail::getVoteItemId).eq(voteItemId);
        List<VoteDetail> voteDetailList = list(queryWrapper);
        List<VoteDetailVO> voteDetailVOList=new ArrayList<>();
        for(VoteDetail voteDetail:voteDetailList){
            VoteDetailVO voteDetailVO=new VoteDetailVO();
            BeanUtil.copyProperties(voteDetail,voteDetailVO);
            queryWrapper = QueryWrapper.create().where(WxUserInfo::getOpenid).eq(voteDetail.getOpenid());
            WxUserInfo wxUserInfo = wxUserInfoService.getOne(queryWrapper);
            voteDetailVO.setWxUserInfo(wxUserInfo);
            voteDetailVOList.add(voteDetailVO);
        }
        System.out.println("voteDetailVOList = " + voteDetailVOList);
        return ResultVO.success(voteDetailVOList);
    }
}
